/*     */ package com.zimbra.cs.mime;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.FileUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.extension.ExtensionUtil;
/*     */ import com.zimbra.cs.mime.handler.NoOpMimeHandler;
/*     */ import com.zimbra.cs.mime.handler.UnknownTypeHandler;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.concurrent.ConcurrentHashMap;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class MimeHandlerManager
/*     */ {
/*  42 */   private static Map<String, HandlerInfo> sHandlers = new ConcurrentHashMap();
/*  43 */   private static Log sLog = LogFactory.getLog(MimeHandlerManager.class);
/*     */   
/*     */   private static class HandlerInfo {
/*     */     MimeTypeInfo mimeType;
/*     */     Class<? extends MimeHandler> clazz;
/*     */     String realMimeType;
/*     */     
/*     */     public MimeHandler getInstance() throws MimeHandlerException {
/*     */       MimeHandler handler;
/*     */       try {
/*  53 */         handler = (MimeHandler)this.clazz.newInstance();
/*     */       } catch (InstantiationException e) {
/*  55 */         throw new MimeHandlerException(e);
/*     */       } catch (IllegalAccessException e) {
/*  57 */         throw new MimeHandlerException(e);
/*     */       }
/*  59 */       handler.setContentType(this.realMimeType);
/*  60 */       handler.setMimeTypeInfo(this.mimeType);
/*  61 */       return handler;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static MimeHandler getMimeHandler(String mimeType, String filename)
/*     */     throws MimeHandlerException
/*     */   {
/*  77 */     sLog.debug("Getting MIME handler for type %s, filename '%s'", new Object[] { mimeType, filename });
/*     */     
/*  79 */     if (!LC.zimbra_enable_text_extraction.booleanValue()) {
/*  80 */       return new NoOpMimeHandler();
/*     */     }
/*     */     
/*  83 */     MimeHandler handler = null;
/*  84 */     if (!StringUtil.isNullOrEmpty(mimeType)) {
/*  85 */       mimeType = Mime.getContentType(mimeType);
/*     */     }
/*  87 */     String extension = FileUtil.getExtension(filename);
/*  88 */     HandlerInfo handlerInfo = (HandlerInfo)sHandlers.get(getKey(mimeType, extension));
/*     */     
/*  90 */     if (handlerInfo == null) {
/*  91 */       handlerInfo = loadHandler(mimeType, extension);
/*  92 */       sHandlers.put(getKey(mimeType, extension), handlerInfo);
/*     */     }
/*     */     
/*     */ 
/*  96 */     handler = handlerInfo.getInstance();
/*  97 */     sLog.debug("Returning MIME handler: %s", new Object[] { handler.getClass().getName() });
/*  98 */     return handler;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static int getIndexedTextLimit()
/*     */   {
/* 107 */     int length = 1048576;
/*     */     try {
/* 109 */       Provisioning prov = Provisioning.getInstance();
/* 110 */       Server server = prov.getLocalServer();
/* 111 */       length = server.getIntAttr("zimbraAttachmentsIndexedTextLimit", length);
/*     */     } catch (ServiceException e) {
/* 113 */       sLog.warn("Unable to determine maximum indexed content length", e);
/*     */     }
/* 115 */     return length;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static synchronized HandlerInfo loadHandler(String mimeType, String extension)
/*     */     throws MimeHandlerException
/*     */   {
/* 127 */     sLog.debug("Loading MIME handler for type %s, extension '%s'", new Object[] { mimeType, extension });
/*     */     
/*     */     try
/*     */     {
/* 131 */       MimeTypeInfo mt = lookUpMimeTypeInfo(mimeType, extension);
/*     */       
/*     */ 
/*     */ 
/* 135 */       if ((mt == null) || (mt.getHandlerClass() == null)) {
/* 136 */         boolean isTextType = (mimeType != null) && ((mimeType.matches("text/.*")) || (mimeType.equalsIgnoreCase("message/rfc822")));
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 142 */         if (isTextType) {
/* 143 */           sLog.debug("Falling back to %s MIME Handler for type %s", new Object[] { "text/plain", mimeType });
/*     */           
/* 145 */           List<MimeTypeInfo> mimeTypeList = Provisioning.getInstance().getMimeTypes("text/plain");
/*     */           
/* 147 */           if (mimeTypeList.size() > 0) {
/* 148 */             mt = (MimeTypeInfo)mimeTypeList.get(0);
/*     */           } else {
/* 150 */             sLog.warn("Unable to load MIME handler for %s", new Object[] { "text/plain" });
/*     */           }
/*     */         }
/*     */         
/*     */ 
/*     */ 
/* 156 */         if ((mt == null) || (mt.getHandlerClass() == null)) {
/* 157 */           sLog.debug("Falling back to %s MIME Handler for type %s", new Object[] { "all", mimeType });
/*     */           
/* 159 */           List<MimeTypeInfo> mimeTypeList = Provisioning.getInstance().getMimeTypes("all");
/*     */           
/* 161 */           if (mimeTypeList.size() > 0) {
/* 162 */             mt = (MimeTypeInfo)mimeTypeList.get(0);
/*     */           } else {
/* 164 */             throw new MimeHandlerException("Unable to load MIME handler for type all");
/*     */           }
/*     */         }
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 171 */       if (mt.getHandlerClass() == null) {
/* 172 */         String msg = String.format("%s not specified for MIME handler %s.", new Object[] { "zimbraMimeHandlerClass", mt.getDescription() });
/*     */         
/* 174 */         throw new MimeHandlerException(msg);
/*     */       }
/*     */       
/*     */ 
/* 178 */       HandlerInfo handlerInfo = new HandlerInfo(null);
/* 179 */       String className = mt.getHandlerClass();
/* 180 */       if (className.indexOf('.') == -1) {
/* 181 */         className = "com.zimbra.cs.mime.handler." + className;
/*     */       }
/* 183 */       handlerInfo.mimeType = mt;
/* 184 */       handlerInfo.realMimeType = mimeType;
/*     */       try {
/* 186 */         handlerInfo.clazz = ExtensionUtil.loadClass(mt.getExtension(), className).asSubclass(MimeHandler.class);
/*     */       }
/*     */       catch (ClassNotFoundException e) {
/* 189 */         sLog.warn("MIME handler %s for %s (%s) not found", new Object[] { className, extension, mimeType });
/*     */         
/* 191 */         handlerInfo.clazz = UnknownTypeHandler.class;
/*     */       }
/* 193 */       return handlerInfo;
/*     */     } catch (ServiceException e) {
/* 195 */       String msg = String.format("Unable to load MIME handler for type %s, extension %s.", new Object[] { mimeType, extension });
/* 196 */       throw new MimeHandlerException(msg, e);
/*     */     }
/*     */   }
/*     */   
/*     */   private static String getKey(String mimeType, String ext) {
/* 201 */     if (mimeType == null) {
/* 202 */       mimeType = "";
/*     */     }
/* 204 */     if (ext == null) {
/* 205 */       ext = "";
/*     */     }
/* 207 */     return mimeType + "," + ext;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static MimeTypeInfo lookUpMimeTypeInfo(String mimeType, String ext)
/*     */     throws ServiceException
/*     */   {
/* 217 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 220 */     List<MimeTypeInfo> mimeTypes = prov.getAllMimeTypes();
/* 221 */     if (mimeTypes.size() == 0) {
/* 222 */       return null;
/*     */     }
/*     */     
/* 225 */     MimeTypeInfo retVal = null;
/* 226 */     int maxPriority = Integer.MIN_VALUE;
/* 227 */     for (MimeTypeInfo mti : mimeTypes) {
/* 228 */       if ((matches(mti, mimeType, ext)) && (mti.getPriority() > maxPriority)) {
/* 229 */         retVal = mti;
/* 230 */         maxPriority = mti.getPriority();
/*     */       }
/*     */     }
/* 233 */     if ((retVal != null) && (retVal.getHandlerClass() == null)) {
/* 234 */       ZimbraLog.mailbox.warn("%s not defined for MIME handler %s", new Object[] { "zimbraMimeHandlerClass", retVal.getDescription() });
/*     */     }
/*     */     
/* 237 */     return retVal;
/*     */   }
/*     */   
/*     */   private static boolean matches(MimeTypeInfo mti, String mimeType, String ext) {
/* 241 */     if (mti == null) {
/* 242 */       return false;
/*     */     }
/* 244 */     if (mimeType == null) {
/* 245 */       mimeType = "";
/*     */     }
/* 247 */     if (ext == null) {
/* 248 */       ext = "";
/*     */     }
/* 250 */     mimeType = mimeType.toLowerCase();
/* 251 */     ext = ext.toLowerCase();
/* 252 */     if (mti.getFileExtensions().contains(ext)) {
/* 253 */       return true;
/*     */     }
/* 255 */     for (String patternString : mti.getMimeTypes()) {
/* 256 */       Pattern pattern = Pattern.compile(patternString);
/* 257 */       Matcher matcher = pattern.matcher(mimeType);
/* 258 */       if (matcher.find()) {
/* 259 */         return true;
/*     */       }
/*     */     }
/* 262 */     return false;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mime/MimeHandlerManager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */